package com.itboye.hutouben.adapter;

import android.app.Activity;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.itboye.hutouben.R;
import com.itboye.hutouben.activity.store.GoPayActivity;
import com.itboye.hutouben.activity.store.OrderActivity;
import com.itboye.hutouben.activity.store.OrderDetailActivity;
import com.itboye.hutouben.base.BaseActivity;
import com.itboye.hutouben.base.MyApplcation;
import com.itboye.hutouben.bean.DingDanQuery;
import com.itboye.hutouben.presenter.OrderPresenter;
import com.itboye.hutouben.util.ByAlert;
import com.itboye.hutouben.util.Const;
import com.itboye.hutouben.util.IsUtilUid;
import com.itboye.hutouben.util.MyGongJv;
import com.itboye.hutouben.util.SPUtils;
import com.itboye.hutouben.util.XImageLoader;
import com.itboye.hutouben.volley.ResultEntity;

import java.util.Iterator;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

/**
 * 订单查询
 */
public class OrderAdapter extends BaseAdapter implements Observer {
    List<DingDanQuery.ListEntity> beanList;
    Activity activity;
    OrderPresenter orderPresenter;
    int type;
    private String tempOrderCode;

    public OrderAdapter(List<DingDanQuery.ListEntity> beanList, Activity activity, int type) {
        this.beanList = beanList;
        this.activity = activity;
        this.type = type;
        orderPresenter = new OrderPresenter(this);
    }

    public void setStatus(int type) {
        this.type = type;
    }

    @Override
    public int getCount() {
        return beanList.size();
    }

    @Override
    public Object getItem(int position) {
        return beanList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View view, ViewGroup parent) {
        ViewHolder holder = null;
        if (view == null) {
            holder = new ViewHolder();
            view = LayoutInflater.from(activity).inflate(R.layout.item_order, null);
            holder.order_img = (ImageView) view.findViewById(R.id.order_img);
            holder.order_geshu = (TextView) view.findViewById(R.id.order_geshu);
            holder.order_title = (TextView) view.findViewById(R.id.order_title);
            holder.order_price = (TextView) view.findViewById(R.id.order_price);
            holder.order_num = (TextView) view.findViewById(R.id.order_num);
            holder.order_money = (TextView) view.findViewById(R.id.order_money);
            holder.order_right = (TextView) view.findViewById(R.id.order_right);
            holder.order_right01= (TextView) view.findViewById(R.id.order_right01);
            holder.order_left = (TextView) view.findViewById(R.id.order_left);
            holder.parent = (LinearLayout) view.findViewById(R.id.parent);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }
        holder.order_geshu.setText("订单号" + beanList.get(position).getOrder_code());
        //    holder.order_title.setText(beanList.get(position).getItems().get(position).getName());
        //    holder.order_price.setText("¥"+beanList.get(position).getItems().get(position).getPrice());
        //   holder.order_num.setText("*"+beanList.get(position).getItems().get(position).getCount());

        List<DingDanQuery.ListEntity.ItemsEntity> item = beanList.get(position).getItems();
        String name = "";
        String price = "";
        String count = "";
        String img = "";
        for (int i = 0; i < item.size(); i++) {
            name = item.get(i).getName();
            price = Double.parseDouble(item.get(i).getPrice()) / 100 + "";
            count = item.get(i).getCount();
            img = item.get(i).getImg();
        }
        holder.order_title.setText(name);
        holder.order_price.setText(price);
        holder.order_num.setText("*"+count);

        XImageLoader.load(Const.IMAGE_HEAD + img,
                holder.order_img);

        holder.order_money.setText("¥" + MyGongJv.zhuanhua(Double.parseDouble(beanList.get(position).getPrice()) / 100));
        if (type == 0) {//全部
            if(beanList.get(position).getQuery_status().equals("1")){
                holder.order_left.setVisibility(View.VISIBLE);
                holder.order_right.setVisibility(View.VISIBLE);
                holder.order_right01.setVisibility(View.GONE);
                holder.order_right.setText(R.string.quzhifu);
                holder.order_left.setText("取消订单");
            }else if(beanList.get(position).getQuery_status().equals("3")){
                holder.order_left.setVisibility(View.GONE);
                holder.order_right.setVisibility(View.VISIBLE);
                holder.order_right01.setVisibility(View.GONE);
                holder.order_left.setText("查看物流");
                holder.order_right.setText("确认收货");
            }else if(beanList.get(position).getQuery_status().equals("2")){
                holder.order_left.setVisibility(View.GONE);
                holder.order_right.setVisibility(View.GONE);
                holder.order_right01.setVisibility(View.VISIBLE);
                holder.order_right01.setText("待发货");
            }else if(beanList.get(position).getQuery_status().equals("4")||beanList.get(position).getQuery_status().equals("7")){
                holder.order_left.setVisibility(View.GONE);
                holder.order_right.setVisibility(View.GONE);
                holder.order_right01.setVisibility(View.VISIBLE);
                holder.order_right01.setText("已完成");
            }else if(beanList.get(position).getQuery_status().equals("8")){
                holder.order_left.setVisibility(View.GONE);
                holder.order_right.setVisibility(View.GONE);
                holder.order_right01.setVisibility(View.VISIBLE);
                holder.order_right01.setText("已关闭");
            }else{
                holder.order_left.setVisibility(View.GONE);
                holder.order_right.setVisibility(View.GONE);
                holder.order_right01.setVisibility(View.GONE);
            }
        }
        if (type == 1) {//待付款
            holder.order_left.setVisibility(View.VISIBLE);
            holder.order_right.setVisibility(View.VISIBLE);
            holder.order_right01.setVisibility(View.GONE);
            holder.order_right.setText(R.string.quzhifu);
            holder.order_left.setText("取消订单");
        }
        if (type == 2) {//待发货
            holder.order_left.setVisibility(View.GONE);
            holder.order_right.setVisibility(View.GONE);
            holder.order_right01.setVisibility(View.GONE);
        }
        if (type == 3) {//待收货
            holder.order_left.setVisibility(View.GONE);
            holder.order_right.setVisibility(View.VISIBLE);
            holder.order_right01.setVisibility(View.GONE);
            holder.order_left.setText(R.string.chakanwuliu);
            holder.order_right.setText(R.string.querenshouhuo);
        }
        if (type == 4) {//已完成
            holder.order_left.setVisibility(View.GONE);
            holder.order_right.setVisibility(View.GONE);
            holder.order_right01.setVisibility(View.GONE);
            holder.order_right.setText(R.string.chakanxiangqing);
        }
        //去支付
        holder.order_right.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TextView textView = (TextView) v;
                if (textView.getText().equals(MyApplcation.ctx.getString(R.string.quzhifu))) {
                    Intent intent = new Intent(activity, GoPayActivity.class);
                    intent.putExtra("order_code", beanList.get(position).getOrder_code());
                    intent.putExtra("time", beanList.get(position).getCreatetime());
                    intent.putExtra("type", "two");//用来标示是首次支付还是重新支付
                    activity.startActivity(intent);
                } else if (textView.getText().equals(MyApplcation.ctx.getString(R.string.chakanwuliu))) {
                    ByAlert.alert("请拨打客服电话询问");
                    //                    String uid= SPUtils.get(activity,null,Const.ID,"")+"";
//                    String password=SPUtils.get(activity,null,Const.PASSWORD,"")+"";
//                    Intent intent = new Intent(activity, WebActivity.class);
//                    intent.putExtra("url", "http://api.ryzcgf.com/public/web.php/Express/index?order_code="+beanList.get(position).getOrder_code()+"&uid="+uid+"&psw="+password);
//                    intent.putExtra("title", "物流信息");//用来标示是首次支付还是重新支付
//                    activity.startActivity(intent);
                } else if (textView.getText().equals(MyApplcation.ctx.getString(R.string.querenshouhuo))) {
                    String uid = SPUtils.get(activity, null, Const.ID, "") + "";
                    String sid = SPUtils.get(MyApplcation.ctx, null, Const.S_ID, "") + "";
                    orderPresenter.querenShouHuo(sid, uid, beanList.get(position).getOrder_code());
                }

            }
        });
        holder.order_left.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TextView textView = (TextView) v;
                if (textView.getText().equals("取消订单")) {
                    tempOrderCode = beanList.get(position).getOrder_code();
                    String uid = SPUtils.get(activity, null, Const.ID, "") + "";
                    orderPresenter.quxiaoDingDan(uid, beanList.get(position).getOrder_code());
                } else {
                    String uid = SPUtils.get(activity, null, Const.ID, "") + "";
                    String sid = SPUtils.get(MyApplcation.ctx, null, Const.S_ID, "") + "";
                    tempOrderCode = beanList.get(position).getOrder_code();
                    orderPresenter.querenShouHuo(sid, uid, beanList.get(position).getOrder_code());
                }

            }
        });
        //点击标题跳转到详情
        holder.order_title.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(activity, OrderDetailActivity.class);
                intent.putExtra("order_code", beanList.get(position).getOrder_code());
                activity.startActivity(intent);
            }
        });
        holder.parent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(activity, OrderDetailActivity.class);
                intent.putExtra("order_code", beanList.get(position).getOrder_code());
                activity.startActivity(intent);
            }
        });

        return view;
    }

    @Override
    public void update(Observable observable, Object data) {
        ResultEntity entity = handlerError(data);
        if (entity != null) {
            IsUtilUid.tianjia(entity);
            if (entity.getEventType() == OrderPresenter.querenshouhuo_success) {
                ByAlert.alert(entity.getData());
                OrderActivity.act.initViewPager02(type);
                Iterator<DingDanQuery.ListEntity> iterator = beanList.iterator();
                while (iterator.hasNext()) {
                    DingDanQuery.ListEntity listEntity = iterator.next();
                    if (listEntity.getOrder_code().equals(tempOrderCode)) {
                        iterator.remove();
                        if (beanList.size() <= 0) {
                            ByAlert.alert("没有相关订单了哦");
                        }
                        notifyDataSetChanged();
                        break;
                    }
                }
            } else if (entity.getEventType() == OrderPresenter.querenshouhuo_fail) {
                ByAlert.alert(entity.getData());
            } else if (entity.getEventType() == OrderPresenter.quxiao_success) {
                ByAlert.alert(entity.getData());
                OrderActivity.act.initViewPager02(type);
                Iterator<DingDanQuery.ListEntity> iterator = beanList.iterator();
                while (iterator.hasNext()) {
                    DingDanQuery.ListEntity listEntity = iterator.next();

                    if (listEntity.getOrder_code().equals(tempOrderCode)) {
                        iterator.remove();
                        if (beanList.size() <= 0) {
                            ByAlert.alert("没有相关订单了哦");
                        }
                        notifyDataSetChanged();
                        break;
                    }
                }
            } else if (entity.getEventType() == OrderPresenter.quxiao_fail) {
                ByAlert.alert(entity.getData());
            }
        }
    }

    public static final String EVENT_TYPE_UNKNOWN = BaseActivity.class
            .getName() + "_unknown";

    /**
     * @param data
     * @return 2016-4-23 hebidu
     */
    public ResultEntity handlerError(Object data) {
        ResultEntity result = (ResultEntity) data;
        if (result == null) {
            result = new ResultEntity("-1", "数据格式错误!~", data);
            return result;
        }

        if (result.hasError()) {
            ByAlert.alert(result.getMsg());
            result.setEventType(EVENT_TYPE_UNKNOWN);
            return result;
        }

        return result;
    }

    class ViewHolder {
        ImageView order_img;
        TextView order_geshu, order_title, order_price, order_num, order_money, order_right, order_left,order_right01;
        LinearLayout parent;
    }
}
